//行政区域管理 显示表格及js实体

Ext.define('AdministrativeArea', {
    extend: 'Ext.data.Model',
    fields: [
        "Id"
		, "Code"   //区域编号
		, "Province"   //省份
		, "City"   //市
		, "County"   //县
		, "Level"   //行政级别
		, "Spell"   //拼音		
	],
    idProperty: "Id"
});

Ext.define('SHApp.System.AdministrativeArea.Grid', {
    extend: 'BLBTSystem.grid',
    alias: 'shapp.system.grid',
    requires: ['SHApp.System.AdministrativeArea.Form',
    'SHApp.System.AdministrativeArea.AreaComboBox'],
    cmbProvinceID: '_cmbProvinceID',
    cmbCityID: '_cmbCityID',
    cmbCountyID: '_cmbCountyID',
    dataModel: "AdministrativeArea",
    sortInfield: 'Code', //排序字段
    dataUrl: "api/administrativearea?format=json",
    initComponent: function () {
        var me = this;
        Ext.apply(this);
        me.btnAddID += me.id;
        me.btnEditID += me.id;
        me.btnDeleteID += me.id;
        me.cmbProvinceID += me.id;
        me.cmbCityID += me.id;
        me.cmbCountyID += me.id;
        this.creatTbar();
        this.creatColumns();
        this.callParent(arguments);
        this.checkBtnPermission(me.pvalue);
    },
    creatColumns: function () {
        var me = this;
        me.columns = [{
            text: "Id",
            flex: 1,
            dataIndex: 'Id',
            sortable: false,
            hidden: true
        }, {
            text: "区域编号",
            width: 100,
            dataIndex: 'Code',
            sortable: true
        }, {
            text: "省份",
            width: 100,
            dataIndex: 'Province',
            sortable: true
        }, {
            text: "市",
            width: 100,
            dataIndex: 'City',
            sortable: true
        }, {
            text: "县",
            width: 100,
            dataIndex: 'County',
            sortable: true
        }, {
            text: "行政级别",
            width: 100,
            dataIndex: 'Level',
            sortable: true
        }, {
            text: "拼音",
            width: 100,
            dataIndex: 'Spell',
            sortable: true
        }]
    },
    creatTbar: function () {
        var me = this;
        me.tbar = [
		    {
		        xtype: 'areaComboBox',
		        fieldLabel: '省份:',
		        labelWidth: 40,
		        name: 'Province',
		        id: me.cmbProvinceID,
		        width: 160,
		        listeners: {
		            select: function (comb, record, opt) {
		                if (record != null && record.length > 0) {
		                    me.down("#" + me.cmbCityID).clearData();
		                    me.down("#" + me.cmbCityID).loadByProvince(record[0].data.Province);
		                }
		            }
		        }
		    }, {
		        xtype: 'areaComboBox',
		        fieldLabel: '市:',
		        labelWidth: 40,
		        name: 'City',
		        valueField: "Code",
		        displayField: "City",
		        id: me.cmbCityID,
		        width: 160
		    }, ' ', {
		        xtype: 'button',
		        text: '查询',
		        iconCls: 'icon-btn-search',
		        handler: function () {
		            me.clearFilters();

		            var vProvince = me.down("#" + me.cmbProvinceID).getDisplayValue();
		            if (vProvince && vProvince.length > 0) {
		                me.addFilter("Province", vProvince);
		            }

		            var vCity = me.down("#" + me.cmbCityID).getDisplayValue();
		            if (vCity && vCity !="区") {
		                me.addFilter("City", vCity);
		            }

		            me.loadByFilter();
		        }
		    }, '->', {
		        xtype: 'button',
		        text: '添加',
		        id: me.btnAddID,
		        iconCls: 'icon-btn-add',
		        handler: function () {
		            var win = me.getForm('SHApp.System.AdministrativeArea.Form');
		            win.loadRecord(new AdministrativeArea({ Id: null }));
		            win.Show("添加");
		        }
		    }, ' ', {
		        xtype: 'button',
		        disabled: true,
		        id: me.btnEditID,
		        text: '修改',
		        iconCls: 'icon-btn-edit',
		        handler: function () {
		            var rs = me.getSelectionModel().getSelection();
		            if (rs) {
		                if (rs.length > 0) {
		                    // rs[0].data.StartDate = formatDate(rs[0].data.StartDate);                           
		                    me.EditData(me.getForm('SHApp.System.AdministrativeArea.Form'));
		                }
		            }
		        }
		    }, ' ', {
		        xtype: 'button',
		        disabled: true,
		        id: me.btnDeleteID,
		        text: '删除',
		        iconCls: 'icon-btn-delete',
		        handler: function () {
		            me.DeleteData();
		        }
		    }
        ]
    }
})

